<template>
    <div class="body">
        <el-dialog
                :title="title"
                :visible.sync="dialogFormVisible"
                width="600px"
                :close-on-click-modal="false"
                @close="close"
        >
            <el-form ref="form" :model="form" label-width="120px">
                <el-form-item label="商品名称" prop="name" v-if="type=='name'">
                    <el-input v-model.trim="form.name" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="价格" prop="price" v-if="type=='price'">
                    <el-input-number v-model="form.price" :precision="2" :step="1" style="width: 300px" :min="0.00"></el-input-number>
                </el-form-item>
                <el-form-item label="库存" prop="stock" v-if="type=='stock'">
                    <el-input-number v-model="form.stock" :min="0" style="width: 300px"></el-input-number>
                </el-form-item>
                <el-form-item label="排序" prop="sort" v-if="type=='sort'">
                  <el-input v-model.trim="form.sort" autocomplete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button size="medium" @click="close">取 消</el-button>
                <el-button size="medium" type="primary" @click="save">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import {goodUsave} from "@/api/good";
    import {mapState} from "vuex";

    export default {
        name: "goodlistedit",
        created() {

        },
        // computed: {
        //     ...mapState(['storeId']),
        // },
        data() {
            return {
                form: {
                    name: "",
                    price: "",
                    stock: "",
                    sort: "",
                },
                type: '',
                // rules: {
                //     username: [
                //         { required: true, trigger: "blur", message: "请输入用户名" },
                //     ],
                //     password: [
                //         { required: true, trigger: "blur", message: "请输入密码" },
                //     ],
                //     email: [{ required: true, trigger: "blur", message: "请输入邮箱" }],
                //     permissions: [
                //         { required: true, trigger: "blur", message: "请选择权限" },
                //     ],
                // },
                title: "",
                dialogFormVisible: false,
                storeId:'',
            }
        },
        components: {},
        methods: {
            showEdit(row, type) {
                if (!row) {
                    this.title = "添加";
                } else {
                    this.title = "编辑";
                    this.form = Object.assign({}, row);
                    this.type = type;
                }
                this.dialogFormVisible = true;
            },
            setStoreId(storeId){
                this.storeId = storeId
            },
            close() {
                this.$refs["form"].resetFields();
                this.form = this.$options.data().form;
                this.dialogFormVisible = false;
            },
            async save() {
                var params={}
                if(this.type=="name"){
                    params={
                        id:this.form.id,
                        name:this.form.name,
                    }
                }else if(this.type=="price"){
                    params={
                        id:this.form.id,
                        price:this.form.price,
                        storeId:this.storeId,
                    }
                }else if(this.type=="stock"){
                    params={
                        id:this.form.id,
                        stock:this.form.stock,
                        storeId:this.storeId,
                    }
                }else if(this.type=="sort"){
                  params={
                    id:this.form.id,
                    sort:this.form.sort,
                    storeId:this.storeId,
                  }
                }
                console.log(params)
                const {msg} = await goodUsave(params);
                this.$baseMessage(msg, "success");
                this.$emit("fetchData");
                this.close();
            },
        }
    };
</script>
